package com.jkl.nettyserver;

import com.jkl.nettyserver.Netty.NettyServer;
import com.jkl.nettyserver.Tool.Zookeeper.ZookeeperClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.InetAddress;
import java.net.UnknownHostException;
@SpringBootApplication
public class NettyServerApplication implements CommandLineRunner {
    final NettyServer nettyServer;
    final ZookeeperClient zookeeperClient;
    @Value("${netty.port}")
    int nettyPort;
    @Value("${server.port}")
    int httpPort;

    @Autowired
    public NettyServerApplication(NettyServer nettyServer,  ZookeeperClient zookeeperClient) {
        this.nettyServer = nettyServer;
        this.zookeeperClient = zookeeperClient;
    }

    public static void main(String[] args) {
        SpringApplication.run(NettyServerApplication.class, args);
    }
    //由于要调用springboot的内置组件，因此整合netty的时候不能注入ServerBootstrap这个Bean来启动netty服务器
    //springboot启动后运行下面的代码
    @Override
    public void run(String... args) throws UnknownHostException {
        //获取本机的ip
        String address="/" +InetAddress.getLocalHost().getHostAddress() + ":" + nettyPort + ":" + httpPort;
        zookeeperClient.register(address);
        nettyServer.start();
    }
}
